/*
 * Inbox.java
 *
 * Created on May 15, 2008, 2:05 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package morseme;

/**
 *
 * @author tt
 */
import javax.wireless.messaging.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class Inbox extends List implements CommandListener {

    private static final Command dispCmd = new Command("Display", Command.OK, 1);
    private static final Command cancelCmd = new Command("Cancel", Command.CANCEL, 1);
    private static Inbox inbox;
    private Vector msgs = new Vector();
    private MainMenu menu;
    private MsgForm msgForm;
    private MorseME midlet;

    public Inbox(MainMenu menu, MorseME midlet) {

        super("Inbox", List.EXCLUSIVE);
        
        this.midlet = midlet;

        this.menu = menu;
        msgForm = new MsgForm(this, midlet);

        this.addCommand(dispCmd);
        this.addCommand(cancelCmd);
        this.setCommandListener(this);

        updateInbox();

        this.inbox = this;
    }

    public void updateInbox() {

// for(int i = 0; i < this.size(); i++){
// this.delete(i);
// }
        this.deleteAll();
        for (int i = 0; i < msgs.size(); i++) {

            Message msg = (Message) msgs.elementAt(i);
            if (msg instanceof TextMessage) {
                TextMessage tmsg = (TextMessage) msg;
                String st = tmsg.getPayloadText();
                if(st.indexOf("none#none#") == -1){
                    st = "[Encrypted Data]";
                }else{
                    st = st.substring(st.indexOf("none#none#")+"none#none#".length());
                }
                
                this.append(st, null);
            }
        }
    }

    public void commandAction(Command c, Displayable d) {

        if (c == dispCmd || c == SELECT_COMMAND) {

            if (this.msgForm != null && this.size() > 0) {

                int index = this.getSelectedIndex();
                Message msg = (Message) msgs.elementAt(index);
                
                this.msgForm.setMessage(msg);

                Display.getDisplay(this.midlet).setCurrent(this.msgForm);
            }

        } else if (c == cancelCmd) {

            Display.getDisplay(MorseME.getMIDlet()).setCurrent(this.menu);
        }
    }
    
    public void removeMessage(Message msg){
        if(msg != null)
            this.msgs.removeElement(msg);
    }
    
    public void decryptMessage(Message msg){
        if(msg != null){
            int index = this.msgs.indexOf(msg);
            
            if(index == -1)
                return;
            
            TextMessage sms = (TextMessage) msg;
            String encryptedMsg = sms.getPayloadText();
            
            do{
                encryptedMsg = encryptedMsg.substring(encryptedMsg.indexOf("#")+1);
            }while(encryptedMsg.indexOf("#") != -1);
            
            sms.setPayloadText("none#none#"+encryptedMsg);
            this.msgs.setElementAt(sms, index);
        }
    }

    public static Inbox getInbox() {
        if (inbox != null) {
            return inbox;
        }
        return null;
    }

    public void addMessage(Message msg) {

        if (!msgs.contains(msg)) {

            msgs.addElement(msg);
        }
    }
}

